package ua.soso.dao.hibernate;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Required;
import ua.soso.dao.FindByIdDao;
import ua.soso.dao.model.IdBean;

/**
 * Created by Babka Aleksey
 * Date: 06.04.12
 */
public abstract class HibernateBaseDao<T extends IdBean> implements FindByIdDao<T>{
    protected final Class<T> entityClass;
    private SessionFactory sessionFactory;

    protected HibernateBaseDao(Class<T> entityClass) {
        this.entityClass = entityClass;
    }

    @Required
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public Session session(){
       return sessionFactory.getCurrentSession();
    }

    @SuppressWarnings("unchecked")
    @Override
    public T findById(long id){
        return (T) session().get(entityClass, id);
    }
}
